<?php

namespace app\common\business;

use app\common\lib\Curl;
use app\common\lib\Status;
use app\common\lib\Str;
use app\common\lib\Time;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\facade\Cache;

class UserInfo
{

    public $model = null;

    public function __construct()
    {
        $this->model = new \app\common\model\mysql\UserInfo();
    }

    /**
     * @param $where
     * @param $page
     * @param $limit
     * @param string $field
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getListByWhere($where,$page,$limit,$field='*'){
        $where[] = ['status','=',config("status.mysql.table_normal")];
        if ($page!=0 && $limit !=0){
            $list = $this->model->getPageListByWhere($where,$page,$limit,$field);
        }else{
            $list = $this->model->getNormalListByWhere($where,$field);
        }
        if($list->isEmpty()){
            return [];
        }
        return $list->toArray();
    }

    public function updateData($user_id, $data)
    {
        $user = (new User())->getNormalUserById($user_id);
        if (!$user) {
            throw new Exception("不存在该用户");
        }

        $info = self::getNormalSimpleInfoByField('user_id',$user_id);
        if (!$info) {
            throw new Exception("不存在该用户");
        }

        $userInfoId = $info['id'];
        try {
            $res = $this->model->updateById($userInfoId,$data);
        } catch (\Exception $e) {
            throw new Exception("数据库内部异常");
        }
        return $res;
    }

    public function getNormalInfoByField($field,$value)
    {
        $user = $this->model->getNormalByField($field,$value);
        $user=!$user || $user->status != config("status.mysql.table_normal")?[]:$user->toArray();
        return self::resetUserInfo($user, (new Category)->categoryDictionaries(),(new WorkType())->categoryDictionaries());
    }

    public function getNormalSimpleInfoByField($field,$value)
    {
        $data = $this->model->getNormalByField($field,$value);
        if (!$data || $data->status != config("status.mysql.table_normal")) {
            return [];
        }
        $data=$data->toArray();
        return $data;
    }

    public function getInfoSkillLabelByField($field,$value)
    {
        $workType=(new WorkType())->getChildDictionaries();
        $data = $this->model->getNormalByField($field,$value);
        if (!$data || $data->status != config("status.mysql.table_normal")) {
            return [];
        }
        $data=$data->toArray();
        if(isset($data['skill_label'])&&!empty($data['skill_label'])){
            $skillLabel=explode(',',$data['skill_label']);
            $skillLabelData=[];
            foreach ($skillLabel as $v){
                $skillLabelData[]=$workType[$v];
            }
            $data['skill_label']=$skillLabelData;
        }else{
            $data['skill_label']=[];
        }
        return $data;
    }

    public static function resetUserInfo($data,$category=[],$workType=[]){
        if(!empty($data)){
            if(!empty($category)){
                $data['education_text']=empty($data['education'])?'':$category[$data['education']];
                $data['experience_text']=empty($data['experience'])?'':$category[$data['experience']];
                $data['free_text']=empty($data['free'])?'':$category[$data['free']];
            }
            if(isset($data['skill_certificate']) && !empty($data['skill_certificate'])){
                $data['skill_certificate']=explode(',',$data['skill_certificate']);
            }
            if(!empty($workType)){
                $data['intention']=empty($data['intention'])?'':$workType[$data['intention']];
                if(!empty($data['skill_label'])){
                    $skillLabelArr=explode(',',$data['skill_label']);
                    $data['skill_label_text']=[];
                    foreach ($skillLabelArr as $k=>$v){
                        $text= $workType[$v] ?? '';
                        if(!empty($text)){
                            $data['skill_label_text'][]=$text;
                        }
                    }
                }
            }
        }
        return $data;
    }
}
